<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Expires", "0");
%>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>网络服务平台</title>
    <link rel="stylesheet" type="text/css" href="/js/easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="/js/easyui/themes/icon.css">
    <script type="text/javascript" src="/js/easyui/jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="/js/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/js/easyui/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript">
        $(function () {
            $('#newsGrid').datagrid({
                title:'草稿列表',
                iconCls:'icon-save',
                width:"100%",
                height:460,
                nowrap:false,
                striped:true,
                collapsible:true,
                url:'/management/news/fetchDraftNews.action',
                queryParams:{"inIndex":"-1", "indexLevel":"-1"},
                remoteSort:false,
                idField:'id',
                frozenColumns:[
                    [
                        {field:'ck', checkbox:true},
                        {"width":350, "field":"title", "title":"标题",
                            formatter:function (value, rowData, rowIndex) {
                                return '<a target="_blank" href="/management/news/showNewsBackend.action?id=' + rowData.id + '">' + value + '</a>';
                            }
                        }
                    ]
                ],
                columns:[
                    [
                        {"width":220, "field":"indexTitle", "title":"首页标题"},
                        {"width":70, "field":"inIndex", "title":"置顶级别",
                            formatter:function (value, rowData, rowIndex) {
                                if (value == '1') {
                                    return '频道';
                                } else if (value == '2') {
                                    return '主页';
                                }

                                return '无';
                            }
                        },
                        {"width":70, "field":"indexLevel", "title":"推荐级别",
                            formatter:function (value, rowData, rowIndex) {
                                if (value == '1') {
                                    return '推荐1';
                                } else if (value == '2') {
                                    return '推荐2';
                                } else if (value == '3') {
                                    return '推荐3';
                                } else if (value == '4') {
                                    return '推荐4';
                                }

                                return '推荐5';
                            }
                        },
                        {"width":30, "field":"published", "title":"发布",
                            formatter:function (value, rowData, rowIndex) {
                                if (value == '1') {
                                    return '是';
                                } else if (value == '0') {
                                    return '否';
                                }

                                return '否';
                            }
                        },
                        {"width":130, "field":"publishDate", "title":"发布日期"},
                        {"width":80, "field":"id", "title":"操作",
                            formatter:function (value, rowData, rowIndex) {
                                return '<a href="newsForm.action?isForDraft=true&id=' + value + ' ">编辑</a>&nbsp;<a onclick="deleteNews(\'' + value + '\');" href="#">删除</a>';
                            }
                        }
                    ]
                ],
                pagination:true,
                rownumbers:true,
                toolbar:[
                    {"iconCls":"icon-add", "handler":function () {
                        bulkApprove();
                    }, "text":"批量审批", "title":"批量审批", "id":"btnAdd"}
                ]
            });

            $('#indexLevel').combobox('setValue', -1);
            $('#inIndex').combobox('setValue', -1);

            var p = $('#newsGrid').datagrid('getPager');
            if (p) {
                $(p).pagination({
                    onBeforeRefresh:function () {
                    }
                });
            }
        });

        function showNews() {
            var rows = $('#newsGrid').datagrid('getSelections');
            var row = $('#newsGrid').datagrid('getSelected');
            if (rows.length == 1) {
                window.open("/management/news/showNews.action?id=" + row.id);
            } else if (rows.length < 1) {
                $.messager.alert('警告', '请选择一条记录!', 'warning');
            }
            else {
                $.messager.alert('警告', '一次只能查看一条记录!', 'warning');
            }
        }

        function bulkApprove() {
            var rows = $('#newsGrid').datagrid('getSelections');
            if (rows.length < 1) {
                $.messager.alert('警告', '请选择记录!', 'warning');
                return;
            }

            var idString = "";
            for(var i=0;i<rows.length;i++){
                idString +=  rows[i].id + "_";
            }

            $.getJSON("/management/news/bulkApproveNews.action?ids=" + idString, function (data1) {
                if (data1.successful) {
                    $.messager.show({
                        title:'提示信息',
                        msg:'批量审批成功！'
                    });

                    deleteSelectedRows('newsGrid');
                } else {
                    $.messager.show({
                        title:'提示信息',
                        msg:'批量审批失败！ ' + data1.errorMessage
                    });
                }
             //   query();
            });
        }

        function deleteSelectedRows(gridId) {
            var rows = $('#' + gridId).datagrid('getSelections');
            var length = rows.length;

            if (length < 1) {
                return;
            }

            for (var i = 0; i < length; i++) {
                var selectedRow = $('#' + gridId).datagrid('getSelected');
                var index = $('#' + gridId).datagrid('getRowIndex', selectedRow);
                $('#' + gridId).datagrid('deleteRow', index);
            }
        }

        function deleteNews(id) {
            $.messager.confirm('删除新闻', '你确定要删除新闻吗？', function (data) {
                if (!data) {
                    return;
                }

                $.getJSON("/management/news/deleteNews.action?id=" + id, function (data1) {
                    if (data1.successful) {
                        $.messager.show({
                            title:'提示信息',
                            msg:'删除新闻成功！'
                        });
                    } else {
                        $.messager.show({
                            title:'提示信息',
                            msg:'删除新闻失败！'
                        });
                    }

                    query();
                });
            });
        }

        function query() {
            var newsTitle = $("#newsTitle").val();
            var newsIndexTitle = $("#newsIndexTitle").val();
            var newsColumn = $("input[name='newsColumn']").val();
            var inIndex = $("input[name='inIndex']").val();
            var indexLevel = $("input[name='indexLevel']").val();
            var queryParams = $('#newsGrid').datagrid('options').queryParams;

            queryParams.newsTitle = newsTitle;
            queryParams.newsIndexTitle = newsIndexTitle;
            queryParams.newsColumn = newsColumn;
            queryParams.inIndex = inIndex;
            queryParams.indexLevel = indexLevel;

            $('#newsGrid').datagrid('options').queryParams = queryParams;
            $("#newsGrid").datagrid('reload');
        }

        function cleanColumn() {
            $("#columnCombo").combotree('setValue', '');
        }

        function getSelected() {
            var selected = $("#newsGrid").datagrid("getSelected");
            return selected;
        }

        function getSelections() {
            var rows = $("#newsGrid").datagrid("getSelections");
            return rows;
        }

        function selectRow(rowNumber) {
            $("#newsGrid").datagrid("selectRow", rowNumber);
        }

    </script>

</head>

<body class="easyui-layout">
<s:if test="null == #session._login_user_">
    <script type="text/javascript">
        top.location.href = '/admin';
    </script>
</s:if>
<div region="center" style="height:600px;padding:10px;background:#efefef;">
    <div id="p" class="easyui-panel" title="新闻搜索" icon="icon-search" style="height:80px;padding:10px;">
        <table style="border: #15428B; border-style: solid; border-width: 1px ">
            <tr>
                <td><label>标题:</label></td>
                <td>&nbsp;<input name="newsTitle" id="newsTitle"/></td>
                <td><label>首页标题:</label></td>
                <td>&nbsp;<input name="newsIndexTitle" id="newsIndexTitle"/></td>
                <td><label>栏目:</label></td>
                <td>&nbsp;<select id="columnCombo" class="easyui-combotree" name="newsColumn"
                                  url="/management/columns/fetchColumnsForTree.action"
                                  cascadeCheck="false" style="width:200px;" panelHeight="400"></select>&nbsp;</td>
                <td><a class="easyui-linkbutton" onclick="cleanColumn()">清除栏目</a>&nbsp;&nbsp;</td>
                <td><label>置顶:</label></td>
                <td>&nbsp;
                    <select id="inIndex" class="easyui-combobox" name="inIndex" style="width:60px;">
                        <option value="-1">所有</option>
                        <option value="0">不推荐</option>
                        <option value="1">频道页</option>
                        <option value="2">首页</option>
                    </select>&nbsp;
                </td>
                <td><label>推荐:</label></td>
                <td>
                    &nbsp; <select id="indexLevel" class="easyui-combobox" name="indexLevel" style="width:60px;">
                    <option value="-1">所有</option>
                    <option value="1">推荐1</option>
                    <option value="2">推荐2</option>
                    <option value="3">推荐3</option>
                    <option value="4">推荐4</option>
                    <option value="5">推荐5</option>
                </select>&nbsp;
                </td>
                <td style="width:20px"></td>
                <td><a class="easyui-linkbutton" onclick="query()">搜索</a></td>
            </tr>
        </table>
    </div>
    <div id="p1" class="easyui-panel" style="height:500px;">`
        <table id="newsGrid"></table>
    </div>
</div>
</body>
</html>